﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Transcend.DP3.Db
{
	public class OracleHelper
	{

		/// <summary>
		/// 从lob中读取数据到stream中
		/// </summary>
		/// <param name="st">输出流</param>
		/// <param name="lob">大对象</param>
		/// <param name="limit">读取字节数限制，-1无限制</param>
		/// <param name="bufSize">缓存区大小</param>
		/// <returns>实际读取的字节数</returns>
		public static long ReadClob(Stream st, ref OracleClob lob, int bufSize, long limit)
		{

			long totalRead = 0;
			if (limit < 0) limit = long.MaxValue;

			if (!lob.IsEmpty)
			{
				int readed = 0;
				byte[] buf = new byte[bufSize];
				while ((readed = lob.Read(buf, 0, bufSize)) > 0)
				{
					totalRead += readed;
					st.Write(buf, 0, readed);
					if (totalRead >= limit) break;
				}
			}

			return totalRead;
		}

		/// <summary>
		/// 从lob中读取数据到stream中
		/// </summary>
		/// <param name="st">输出流</param>
		/// <param name="lob">大对象</param>
		/// <param name="limit">读取字节数限制，-1无限制</param>
		/// <param name="bufSize">缓存区大小</param>
		/// <returns>实际读取的字节数</returns>
		public static long ReadBlob(Stream st, ref OracleBlob lob, int bufSize, long limit)
		{

			long totalRead = 0;
			if (limit < 0) limit = long.MaxValue;

			if (lob != null && !lob.IsEmpty)
			{
				int readed = 0;
				byte[] buf = new byte[bufSize];
				while ((readed = lob.Read(buf, 0, bufSize)) > 0)
				{
					totalRead += readed;
					st.Write(buf, 0, readed);
					if (totalRead >= limit) break;
				}
			}

			return totalRead;
		}

	}
}
